import { createRouter, createWebHistory } from 'vue-router'
import {authService} from '@/service/AuthService'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/main', name: "main", component: () => import('@/views/Main.vue'), meta:{ requiresAuth: true } },
    { path: '/login', name: "login", component: () => import('@/views/Login.vue') },
    { path: '/index', name: "index", component: () => import('@/views/index.vue') },
    { path: "/", redirect: "/main"},
    { path: '/order', name: "order", component: () => import('@/views/order.vue') },
    { path: '/discover', name: "discover", component: () => import('@/views/discover.vue') },
    { path: '/my', name: "my", component: () => import('@/views/my.vue') },
  ],
})


router.beforeEach((to, from) => {
  if (to.meta.requiresAuth && !authService.isLoggedIn) {
    return {name: "login"};
  }
})

export default router
